package net.hualisheng.netty.serialize;


import org.jboss.marshalling.MarshallerFactory;
import org.jboss.marshalling.Marshalling;
import org.jboss.marshalling.MarshallingConfiguration;

import io.netty.handler.codec.marshalling.DefaultMarshallerProvider;
import io.netty.handler.codec.marshalling.DefaultUnmarshallerProvider;
import io.netty.handler.codec.marshalling.MarshallerProvider;
import io.netty.handler.codec.marshalling.MarshallingDecoder;
import io.netty.handler.codec.marshalling.MarshallingEncoder;
import io.netty.handler.codec.marshalling.UnmarshallerProvider;

public  class MarshallingCodeFactory {

	public static MarshallingDecoder marshallingDecoder() {
		// 首先通过Marshalling工具类的方法获取Marshalling实例对象 参数serial标识创建的是java序列化工厂对象。
		 MarshallerFactory factory = Marshalling.getProvidedMarshallerFactory("serial");
		// 创建了MarshallingConfiguration对象，配置了版本号为5  
		 MarshallingConfiguration config = new MarshallingConfiguration();
		 //这个版本号，暂时没搞明白是怎么来指定区分的----------但是特别重要，如果配错了，netty不会报错，但是服务端却收不到数据
		config.setVersion(5);
		UnmarshallerProvider provider = new DefaultUnmarshallerProvider(factory, config);
		// 构建Netty的MarshallingDecoder对象，俩个参数分别为provider和单个消息序列化后的最大长度  
		return new MarshallingDecoder(provider);
	}

	
	public static MarshallingEncoder marshallingEncoder() {
		 MarshallerFactory factory = Marshalling.getProvidedMarshallerFactory("serial");
		 MarshallingConfiguration config = new MarshallingConfiguration();
		config.setVersion(5);
		MarshallerProvider provider = new DefaultMarshallerProvider(factory, config );
		// 构建Netty的MarshallingEncoder对象，MarshallingEncoder用于实现序列化接口的POJO对象序列化为二进制数组 
		return new MarshallingEncoder(provider);
	}
}
